//
//  MYGScheduleSearchViewController.swift
//  MYDoctor
//
//  Created by 王振明 on 2024/5/8.
//

import UIKit

class MYGScheduleSearchViewController: MYGBaseController {
    private var sectionArr:Array<MYGScheduleModel> = []
    private var finishArr:[MYGScheduleListModel] = []
    override func viewDidLoad() {
        super.viewDidLoad()
        configUI()
    }
    @objc func getAllSchedules(){
        MMYGNetwork<MYGScheduleAPI, MYGScheduleBaseModel>.request(target: .scheduleList(params: ["doctor_id":MYGUserManager.user?.doctor.id ?? 0,"search":headSearchBar.text ?? ""])) {[weak self] result in
            self?.sectionArr.removeAll()
            let mode:MYGScheduleBaseModel = result ?? MYGScheduleBaseModel()
            self?.sectionArr = mode.data
            if self?.sectionArr.count ?? 0 > 0{
                self?.noneView.isHidden = true
                for ind in 0...(self?.sectionArr.count)! - 1{
                    var arr:[MYGScheduleListModel] = self?.sectionArr[ind].lists ?? []
                    var newArr:[MYGScheduleListModel] = []
                    for sec in 0...arr.count - 1 {
                        let mod:MYGScheduleListModel = arr[sec]
                        if mod.schedule_status == 3 {
                            self?.finishArr.append(mod)
                        }else{
                            newArr.append(mod)
                        }
                    }
                    let mo:MYGScheduleModel = (self?.sectionArr[ind])!
                    mo.lists = newArr
                    self?.sectionArr[ind] = mo
                }
                var newSectionArr:[MYGScheduleModel] = []
                
                for ind in 0...(self?.sectionArr.count)! - 1{
                    let mode:MYGScheduleModel = self?.sectionArr[ind] ?? MYGScheduleModel()
                    if mode.lists.count > 0{
                        newSectionArr.append(mode)
                    }
                }
                self?.sectionArr = newSectionArr
            }else{
                self?.noneView.isHidden = false
            }
            self?.scheduleTable.reloadData()
            MYGLoading.hide()
        } failed: { _, error in
            MYGLoading.hide()
            MMYGHUD.showToast(text: error)
        }
    }
    private func configUI(){
        self.view.backgroundColor = .white
        view.addSubview(headImageV)
        headImageV.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.height.equalTo(statusBarHeight + 44)
            make.top.equalToSuperview()
        }
        view.addSubview(headSearchBar)
        headSearchBar.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(16.5)
            make.height.equalTo(30)
            make.right.equalToSuperview().offset(-65)
            make.bottom.equalTo(headImageV.snp.bottom).offset(-6)
        }
        view.addSubview(cancelBtn)
        cancelBtn.snp.makeConstraints { make in
            make.left.equalTo(headSearchBar.snp.right)
            make.right.equalToSuperview()
            make.centerY.equalTo(headSearchBar)
            make.height.equalTo(30)
        }
        view.addSubview(noneView)
        noneView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.size.equalTo(CGSize(width: 192, height: 132))
        }
        noneView.addSubviews([noneImageView,noneLab1,noneLab2])
        noneImageView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview()
            make.size.equalTo(CGSize(width: 70.5, height: 68))
        }
        noneLab1.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(noneImageView.snp.bottom).offset(17)
            make.height.equalTo(22.5)
        }
        noneLab2.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(noneLab1.snp.bottom).offset(8)
            make.height.equalTo(16.5)
        }
        view.addSubview(scheduleTable)
        scheduleTable.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(headImageV.snp.bottom)
        }
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.isNavigationBarHidden = true
        self.navigationController?.navigationBar.isHidden = true
    }
    private lazy var headImageV:UIImageView = {
        let cont = UIImageView()
        cont.image = UIImage(named: "schedule_head")
        return cont
    }()
    private lazy var cancelBtn:UIButton = {
        let cont = UIButton()
        cont.setTitle("取消", for: .normal)
        cont.titleLabel?.font = .regular(16)
        cont.setTitleColor(.white, for: .normal)
        cont.addTarget(self, action: #selector(cancelAction), for: .touchUpInside)
        return cont
    }()
    private lazy var headSearchBar:UITextField = {
       let cont = UITextField()
        cont.placeholder = "搜索日程、类型、日期时间和紧急程度"
        cont.backgroundColor = .rgba(255, 255, 255)
        cont.font = .regular(13)
        cont.returnKeyType = .search
        cont.delegate = self
        cont.layerCornerRadius = 15
        let leftV = UIView(frame: CGRect(x: 0, y: 0, width: 40, height: 30))
        let leftImageView = UIImageView(image: UIImage(named: "Chat/search"))
        leftImageView.frame = CGRect(x: 13, y: 8, width: 14, height: 14)
        leftV.addSubview(leftImageView)
        cont.leftView = leftV
        cont.leftViewMode = .always
        return cont
    }()
    private lazy var noneView:UIView = {
        let cont = UIView()
        cont.backgroundColor = .clear
        return cont
    }()
    private lazy var noneImageView:UIImageView = {
        let cont = UIImageView()
        cont.image = UIImage(named: "schedule_searchNone")
        return cont
    }()
    private lazy var noneLab1: UILabel = {
        let label = UILabel()
        label.textColor = .rgba(47, 47, 77, 1)
        label.font = .medium(16)
        label.text = "日程太多？那就直接搜索吧"
        return label
    }()
    private lazy var noneLab2: UILabel = {
        let label = UILabel()
        label.textColor = .rgba(47, 47, 77, 0.5)
        label.font = .medium(12)
        label.text = "在搜索框中输入你想要查找的内容"
        return label
    }()
    private lazy var scheduleTable: UITableView = {
        let cont = UITableView(frame: CGRectZero, style: .grouped)
        cont.delegate = self
        cont.dataSource = self
        cont.separatorStyle = .none
        cont.backgroundColor = .clear
        return cont
    }()
}
extension MYGScheduleSearchViewController:UITextFieldDelegate{
    @objc private func cancelAction(){
        self.navigationController?.popViewController()
    }
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        if textField.text?.count ?? 0 > 0 {
            getAllSchedules()
        }
        return true
    }
}
extension MYGScheduleSearchViewController:UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let sche:MYGScheduleModel = sectionArr[section]
        return sche.lists.count
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return sectionArr.count
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 35
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 120
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerV = UIView()
        view.backgroundColor = .clear
        let sche:MYGScheduleModel = sectionArr[section]
        let titleL = UILabel()
        titleL.text = sche.year
//        titleL.text = "222"
        titleL.font = .medium(17)
        titleL.textColor = .rgba(34, 34, 34)
        headerV.addSubview(titleL)
        titleL.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(20)
            make.centerY.equalToSuperview()
            make.height.equalTo(24)
        }
        return headerV
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = MYGScheduleTableViewCell(style: .default, reuseIdentifier: "MYGScheduleTableViewCell")
        cell.selectionStyle = .none
        let sche:MYGScheduleModel = sectionArr[indexPath.section]
        let scheDetail:MYGScheduleListModel = sche.lists[indexPath.row]
        cell.model = scheDetail
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let vc = MYGMeetingsViewController()
        self.navigationController?.pushViewController(vc)
    }
}
